
package com.craterhill.java.ems.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.craterhill.java.ems.common.beans.Position;
import com.craterhill.java.ems.common.beans.Section;
import com.craterhill.java.ems.dao.SectionDao;

@Repository
public class SectionDaoImpl extends AbstractDao<Section> implements SectionDao {

	@Autowired
	public SectionDaoImpl(final DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public List<Section> findByClasses(final int classId) {
		return super.findMultipleObject(FIND_SECTIONS_BY_CLASS, new SectionExtractor(), classId);
	}

	@Override
	public boolean save(final int classId, final List<Section> sections) {
		return super.batchOperation(SAVE_SECTIONS, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				Section section = sections.get(position);
				preparedStatement.setInt(1, classId);
				preparedStatement.setString(2, section.getSectionName());
				preparedStatement.setInt(3, section.getTeacherId());
				preparedStatement.setInt(4, section.getCreatedBy());
			}

			@Override
			public int getBatchSize() {
				return sections.size();
			}
		});
	}

	@Override
	public boolean remove(final Integer... classIds) {
		return super.batchOperation(DELETE_SECTIONS, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement preparedStatement, int position) throws SQLException {
				preparedStatement.setInt(1, classIds[position]);
			}

			@Override
			public int getBatchSize() {
				return classIds.length;
			}
		});
	}

	@Override
	public Position find(int recordId) {
		return null;
	}

	@Override
	public List<Position> findAll() {
		return null;
	}

	@Override
	public int save(Position t) {
		return 0;
	}

	@Override
	public boolean update(Position t) {
		return false;
	}
}
